package com.asion.vmovie.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.asion.vmovie.R;
import com.asion.vmovie.adapters.MuHouViewAdapter;
import com.asion.vmovie.beans.MuHouTabBean;
import com.asion.vmovie.ui.MVPContract.MuHouContract;
import com.asion.vmovie.ui.MVPModel.MuHouModel;
import com.asion.vmovie.ui.MVPPresenter.MuHouTabPresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by asion on 16-10-18.
 */

public class MuHouFragment extends BaseFragment<MuHouTabPresenter,MuHouModel> implements MuHouContract.TabView{

    @BindView(R.id.muhou_search)
    ImageView muhouSearch;
    @BindView(R.id.muhou_tablayout)
    TabLayout muhouTablayout;
    @BindView(R.id.muhou_viewpager)
    ViewPager muhouViewpager;

    private List<String> idList = new ArrayList<>();
    private List<String> nameList = new ArrayList<>();
    private MuHouViewAdapter muHouViewAdapter;

    public static MuHouFragment newInstance(){
        return new MuHouFragment();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.muhou_fragment;
    }


    @Override
    public void initPresenter() {
        mPresenter.setVM(this,mModel);
        mPresenter.getTabData();
    }

    @Override
    protected void initView() {

        muhouTablayout.setupWithViewPager(muhouViewpager);
        startProgressDialog();
    }


    @OnClick({R.id.muhou_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.muhou_search:
                EventBus.getDefault().post("search");
                break;
        }
    }

    @Override
    public void showDataSuccess(MuHouTabBean muHouTabBean) {
        List<MuHouTabBean.DataBean> data = muHouTabBean.getData();
        for (MuHouTabBean.DataBean dataBean : data) {
            idList.add(dataBean.getCateid());
            nameList.add(dataBean.getCatename());
        }
        muHouViewAdapter = new MuHouViewAdapter(getChildFragmentManager(),nameList,idList);
        muhouViewpager.setAdapter(muHouViewAdapter);
        stopProgressDialog();
    }

    @Override
    public void showDataError(Throwable throwable) {

    }
}
